package com.xinjian.jianba.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;

/**
 * jianba库配置.
 *
 *
 */
@Configuration
@MapperScan(basePackages = JianBaDwmidDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "jianbaDwmidSqlSessionFactory")
public class JianBaDwmidDataSourceConfig {

	/**
	 * 包路径.
	 */
	protected static final String PACKAGE = "com.xinjian.jianba.dao.dwmid";


	@Value("${jianba.db.dwmid.driverClassName}")
	private String jianBaDbDriverClassName;

	@Value("${jianba.db.dwmid.url}")
	private String dbUrl;
	@Value("${jianba.db.dwmid.username}")
	private String dbUser;
	@Value("${jianba.db.dwmid.password}")
	private String dbPassword;

	@Value("${spring.datasource.initialSize}")
	private int initialSize;

	@Value("${spring.datasource.minIdle}")
	private int minIdle;

	@Value("${spring.datasource.maxActive}")
	private int maxActive;

	@Value("${spring.datasource.maxWait}")
	private int maxWait;

	@Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
	private int timeBetweenEvictionRunsMillis;

	@Value("${spring.datasource.minEvictableIdleTimeMillis}")
	private int minEvictableIdleTimeMillis;

	@Value("${spring.datasource.validationQuery}")
	private String validationQuery;

	@Value("${spring.datasource.testWhileIdle}")
	private boolean testWhileIdle;

	@Value("${spring.datasource.testOnBorrow}")
	private boolean testOnBorrow;

	@Value("${spring.datasource.testOnReturn}")
	private boolean testOnReturn;

	@Value("${spring.datasource.poolPreparedStatements}")
	private boolean poolPreparedStatements;

	@Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
	private int maxPoolPreparedStatementPerConnectionSize;

	@Value("${spring.datasource.filters}")
	private String filters;

	@Value("${spring.datasource.connectionProperties}")
	private String connectionProperties;
	
	/**
	 * jianbaDataSource.
	 */
	@Bean(name = "jianbaDwmidDataSource")
	public DataSource jianbaDataSource() {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName(jianBaDbDriverClassName);
		dataSource.setUrl(dbUrl);
		dataSource.setUsername(dbUser);
		dataSource.setPassword(dbPassword);

		dataSource.setInitialSize(initialSize);
		dataSource.setMinIdle(minIdle);
		dataSource.setMaxActive(maxActive);
		dataSource.setMaxWait(maxWait);
		dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
		dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
		dataSource.setValidationQuery(validationQuery);
		dataSource.setTestWhileIdle(testWhileIdle);
		dataSource.setTestOnBorrow(testOnBorrow);
		dataSource.setTestOnReturn(testOnReturn);
		dataSource.setPoolPreparedStatements(poolPreparedStatements);
		dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);

		return dataSource;
	}

	

    /**
     * adsTransactionManager.
     */
    @Bean(name = "jianbaDwmidTransactionManager")
    @Primary
    public DataSourceTransactionManager adsTransactionManager(
            @Qualifier("jianbaDwmidDataSource") DataSource customerDataSource) {
        return new DataSourceTransactionManager(customerDataSource);
    }

	/**
	 * sessionFactory.
	 */
	@Bean(name = "jianbaDwmidSqlSessionFactory")
	@Primary
	public SqlSessionFactory rdsSqlSessionFactory(@Qualifier("jianbaDwmidDataSource") DataSource jianbaDataSource)
			throws Exception {
		final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		sessionFactory.setMapperLocations(resolver.getResources("classpath:mybatis/com/xinjian/jianba/dwmid/*.xml"));
		sessionFactory.setDataSource(jianbaDataSource);
		return sessionFactory.getObject();
	}
}
